﻿//-----------------------------------------------------------------------
// <copyright file="GetTime.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY 
// KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
// </copyright>
// <author>Mark Phillips</author>
// <email>v-mphill</email>
// <date>2004-05-27</date>
// <summary>Gets the current time.
// </summary>
//-----------------------------------------------------------------------

namespace Microsoft.Sdc.Tasks.Time
{
    using System;
    using Microsoft.Build.Framework;

    /// <summary>
    /// Returns the current localised time.
    /// </summary>
    /// <remarks>
    /// <code><![CDATA[
    /// <Time.GetTime Format="yyyyMMdd-HHmmss">
    ///     <Output TaskParameter="Time" PropertyName="ResultsItem" />
    /// </Time.GetTime>]]></code>
    /// <para>where:</para>
    /// <para><i>Format</i></para>
    /// <para>An optional parameter that is the format to return the date and time in. Default is "T" (Long Time)</para>
    /// <para><i>Time (Output)</i></para>
    /// <para>Stores the current time.</para>
    /// </remarks>
    /// <example>
    /// <code><![CDATA[
    /// <Project>
    ///     <Target Name="Test" >
    ///         <Time.GetTime>
    ///             <Output TaskParameter="Time" PropertyName="ResultsItem" />
    ///         </Time.GetTime>
    ///     </Target>
    /// </Project> 
    /// ]]></code>    
    /// </example>
    public class GetTime : TaskBase
    {
        private string currentTime = String.Empty;
        private string format = "T";

        /// <summary>
        /// Optional parameter for the date format to be returned
        /// </summary>
        public string Format
        {
            get
            {
                return this.format;
            }
            set
            {
                this.format = value;
            }
        }

        /// <summary>
        /// Performs the action of this task.
        /// </summary>
        protected override void InternalExecute()
        {
            #region Execute code

            this.currentTime = DateTime.Now.ToString(this.format, null);

            #endregion
        }

        [Output]
        public string Time
        {
            get
            {
                return (currentTime == null ? String.Empty : currentTime);
            }
            set
            {
                currentTime = value;
            }
        }
    }
}
